package xin.pms.service;

import xin.common.utils.AssertUtils;
import xin.common.utils.StringUtils;
import xin.pms.model.Permission;
import xin.pms.model.Role;
import xin.pms.repository.PermissionRepository;
import xin.pms.repository.RoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xin.pms.request.RoleRequest;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <pre>
 * Role Service
 * </pre>
 *
 * @author lixin_ma@outlook.com
 * @version 1.0
 * @since 2018/7/28 16:21
 */
@Service
@Transactional(readOnly = true)
public class RoleService {

    @Autowired
    private RoleRepository repository;

    @Autowired
    private PermissionRepository permissionRepository;

    public void add(RoleRequest request){
        Role role = new Role(request);
        role.setPriority(getMaxPriority());
        if(request.getParams() != null && !request.getParams().isEmpty()){
            role.setPermissions(request.getParams().stream()
                    .map(item->{
                        Permission permission = permissionRepository.findOne(item.getCode());
                        return permission;
                    })
                    .collect(Collectors.toList()));
        }
        repository.save(role);
    }


    /**
     * physical del
     * @param roleId
     */
    @Transactional
    public void del(String roleId){
        Role role = repository.findOne(roleId);
        if(role == null){
            throw new RuntimeException("record not exist");
        }
        repository.delete(roleId);
    }

    public void update(RoleRequest request){
        Role role = repository.findOne(request.getCode());
        if(role == null){
            throw new RuntimeException("record not exist");
        }

        if(StringUtils.isNotBlank(request.getName()))
            role.setName(request.getName());
        if(StringUtils.isNotBlank(request.getDescription()))
            role.setDescription(request.getDescription());
        if(request.getParams() != null && !request.getParams().isEmpty()){
            role.setPermissions(request.getParams().stream()
                    .map(item->{
                        Permission permission = permissionRepository.findOne(item.getCode());
                        if(permission == null){
                            throw new RuntimeException("record not exist");
                        }
                        return permission;
                    })
                    .collect(Collectors.toList()));
        }
        repository.save(role);
    }

    private Integer getMaxPriority(){
        List<Role> roles = repository.getMaxPriority();
        Integer priority = 1;
        if(AssertUtils.isNotNull(roles)){
            priority = roles.get(0).getPriority() + 1;
        }
        return priority;
    }


    public Role findOne(String roleId) {
        Role role =  repository.findOne(roleId);
        if(role == null){
            throw new RuntimeException("record not exist");
        }
        return role;
    }
}